package com.zhang.learn.file;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

/**
 * 学习文件的使用
 * @author zhanghaolian
 * @date 2021/6/26 0:22
 */
public class LearnFile {

    @Test
    public void test() throws IOException {
        String property = System.getProperty("user.dir");
        System.out.println("user.dir: " + property);

        File file = new File(property);
        System.out.println(file.getPath());
        System.out.println(file.getCanonicalPath());
    }

    /**
     * 得出一个结论：java 创建一个file的时，传一个相对路径
     * 那么这个相对路劲就是当前的项目的根目录的
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        String path = "abc.txt";
        File file = new File(path);

        System.out.println(file.getPath());
        System.out.println(file.getCanonicalPath());
        System.out.println(file.getAbsolutePath());

    }

    @Test
    public void test2() throws IOException {
        System.out.println(System.getProperty("java.ext.dirs"));

        ServerSocket serverSocket = new ServerSocket(8200);

        Socket accept = serverSocket.accept();


    }

    /**
     * 9x9乘法表
     */
    @Test
    public void test3() {
        for (int i=1;i<=9;i++) {
            for (int j = 1;j<=i;j++) {
                System.out.print(j + "x" + i + "=" + j*i + "\t");
            }
            System.out.println();
        }
    }

    /**
     * 测试阶乘
     */
    @Test
    public void test4() {
        int j = 5;
        int k = j << 1;

        System.out.println("k="+k);

        int i = 5;
        System.out.println(i + "的阶乘结果为：" + fun(i));
    }
    public int fun(int i)  {
        if (i == 1) {
            return 1;
        }else {
            return i*fun(i-1);
        }
    }

    @Test
    public void test5() {
        //通过classLoader获取资源""表示classpath 路径
        //通过classLoader获取资源"/"表示null
        URL resource = this.getClass().getClassLoader().getResource("logging.properties");
        File logFile = new File(resource.getPath());
        if (logFile.exists()) {
            System.out.println("get login.properties file success");
            System.out.println("aa");
        }


        URL resource2 = this.getClass().getClassLoader().getResource("/");



        System.out.println(resource);
        System.out.println(resource2);
    }


}
